package com.briup.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.reggie.common.R;
import com.briup.reggie.dto.SetmealDto;
import com.briup.reggie.entity.Setmeal;
import com.briup.reggie.service.SetmealDishService;
import com.briup.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.page(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 根据id查询套餐信息和菜品信息
     * （回显）
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithSetmeal(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<SetmealDto> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithSetmeal(setmealDto);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, @RequestParam List<Long> ids) {
        setmealService.setStatus(status,ids);
        return R.success("套餐修改成功");
    }

    /**
     * 删除套餐，同时删除套餐关联的菜品数据
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWiteDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     *  根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> list = setmealService.list(setmeal);
        return R.success(list);
    }
}
